class Solution {
    public int coinChange(int[] coins, int amount) {
        int[] dp=new int[amount+1];
        int max=Integer.MAX_VALUE;
        for(int i=0;i<=amount;i++){
            dp[i]=max;
        }
        dp[0]=0;
        for(int i=0;i<=amount;i++){
            for(int j=0;j<coins.length;j++){
                if(i>=coins[j]&&dp[i-coins[j]]!=max){
                    dp[i]=Math.min(dp[i],dp[i-coins[j]]+1);
                }
                
            }
        }

        return dp[amount]==max?-1:dp[amount];
    }
}